
package com.starshineworks.ajax;

import com.starshineworks.util.*;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet(name = "Ajx", urlPatterns = {"/Ajx"})
public class Ajx extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest request, 
                         HttpServletResponse response)
            throws ServletException, IOException {
        String y = "";
        String op = request.getParameter("op");
        if (op.equals("menu")) {
            response.setContentType("text/html");
            Map<String,String[]> m = request.getParameterMap();
            Integer menuid = new Integer(request.getParameter("menuid"));
            Integer rowid = new Integer(request.getParameter("rowid"));
            String  which = request.getParameter("which");
            ObjectBuilder ob = new ObjectBuilder();
            y = ob.buildMenu(which, rowid, menuid, m);
        }
        else if (op.equals("list")) {
            response.setContentType("application/json");
            String type = request.getParameter("type");
            ObjectBuilder ob = new ObjectBuilder();
            y = ob.buildList(type, Util.HTMLViewTable, false);
        }        
        else if (op.equals("form")) {
            response.setContentType("application/json");
            String type = request.getParameter("type");
            ObjectBuilder ob = new ObjectBuilder();
            y = ob.buildList(type, Util.HTMLViewForm, true);
        }        
        else if (op.equals("sessattr")) {
            String key = request.getParameter("key");
            String val = request.getParameter("val");
            request.getSession().setAttribute(key, Integer.parseInt(val));
            y = "1";
        }
        else {
            y = "";
        }
        PrintWriter out = response.getWriter();
        out.println(y);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException 
    {
        response.setContentType("text/html");
        String y = "";
        PrintWriter out = response.getWriter();
        out.println(y);
    }

}
